package cn.micro.system.manage.annotate.impl;


import cn.micro.system.manage.annotate.UniqueRole;
import cn.micro.system.manage.entity.SysDimRole;
import cn.micro.system.manage.service.SysDimRoleService;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class RoleValidation <T extends Annotation> implements ConstraintValidator<T, SysDimRole> {

    protected Predicate<SysDimRole> predicate = c -> true;

    @Resource
    protected SysDimRoleService sysDimRoleService;

    @Override
    public boolean isValid(SysDimRole sysDimRole, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimRoleService == null || predicate.test(sysDimRole);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class UniqueRoleValidate extends RoleValidation<UniqueRole>{
        @Override
        public void initialize(UniqueRole constraintAnnotation) {
            predicate = c -> sysDimRoleService.getByCodeOrName(c).isEmpty();
        }
    }




}
